home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C08 / ConstPointer.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  901 b   |  41 lines

  1. //: C08:ConstPointer.cpp
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. // Constant pointer arg/return
  7.  
  8. void t(int*) {}
  9.  
  10. void u(const int* cip) {
  11. //!  *cip = 2; // Illegal -- modifies value
  12.   int i = *cip; // OK -- copies value
  13. //!  int* ip2 = cip; // Illegal: non-const
  14. }
  15.  
  16. const char* v() {
  17.   // Returns address of static character array:
  18.   return "result of function v()";
  19. }
  20.  
  21. const int* const w() {
  22.   static int i;
  23.   return &i;
  24. }
  25.  
  26. int main() {
  27.   int x = 0;
  28.   int* ip = &x;
  29.   const int* cip = &x;
  30.   t(ip);  // OK
  31. //!  t(cip); // Not OK
  32.   u(ip);  // OK
  33.   u(cip); // Also OK
  34. //!  char* cp = v(); // Not OK
  35.   const char* ccp = v(); // OK
  36. //!  int* ip2 = w(); // Not OK
  37.   const int* const ccip = w(); // OK
  38.   const int* cip2 = w(); // OK
  39. //!  *w() = 1; // Not OK
  40. } ///:~
  41.